package com.example.administrator.yuejian2.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.ViewFlipper;

import com.example.administrator.yuejian2.R;
import com.example.administrator.yuejian2.other.GlideImageLoader;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class IndexFragment extends Fragment {

    private static final String URl="http://10.0.2.2/YueJian02/posters/";
    private Banner banner;
    private List<String> mListImage;
    private ViewFlipper viewFlipper;
    private RadioButton activity_radio,match_radio,train_radio;
    private ActivityFragment activityFragment;
    private MatchFragment matchFragment;
    private TrainFragment trainFragment;
    private FragmentTransaction fragmentTransaction;
    private FragmentManager fragmentManager;
    public IndexFragment() {
        // Required empty public constructor
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }
    private void initView(View view)
    {
        banner=view.findViewById(R.id.index_banner);
        viewFlipper=view.findViewById(R.id.viewFlipper);
        activity_radio=view.findViewById(R.id.activity_radio);
        match_radio=view.findViewById(R.id.match_radio);
        train_radio=view.findViewById(R.id.train_radio);
        viewFlipper.addView(View.inflate(getActivity(), R.layout.scroll_textview, null));
        viewFlipper.addView(View.inflate(getActivity(),R.layout.scroll_textview2,null));
        viewFlipper.setAutoStart(true);
        banner.setBannerStyle(BannerConfig.NUM_INDICATOR);
        banner.setImageLoader(new GlideImageLoader());
        mListImage = new ArrayList<>();
        mListImage.add(URl+"pic1.jpg");
        mListImage.add(URl+"pic2.jpg");
        mListImage.add(URl+"pic3.jpg");
        mListImage.add(URl+"pic4.jpg");
        banner.setImages(mListImage);
        banner.setBannerAnimation(Transformer.DepthPage);
        banner.setDelayTime(2000);
        banner.setIndicatorGravity(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
        banner.start();
        initEvents();
    }

    private void initEvents()
    {
            activity_radio.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked)
                    {
                        if (activityFragment==null)
                        {
                            activityFragment=new ActivityFragment();
                        }
                        fragmentTransaction=fragmentManager.beginTransaction();
                        fragmentTransaction.replace(R.id.index_fragmentlayout,activityFragment);
                        fragmentTransaction.commit();
                    }
                }
            });

            match_radio.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked)
                    {
                        if (trainFragment==null)
                        {
                            trainFragment=new TrainFragment();
                        }
                        fragmentTransaction=fragmentManager.beginTransaction();
                        fragmentTransaction.replace(R.id.index_fragmentlayout,trainFragment);
                        fragmentTransaction.commit();
                    }
                }
            });

            train_radio.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked)
                    {


                        if (matchFragment==null)
                        {
                            matchFragment=new MatchFragment();
                        }
                        fragmentTransaction=fragmentManager.beginTransaction();
                        fragmentTransaction.replace(R.id.index_fragmentlayout,matchFragment);
                        fragmentTransaction.commit();
                    }
                }
            });

        fragmentManager=getActivity().getSupportFragmentManager();
        activity_radio.setChecked(true);
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v= inflater.inflate(R.layout.fragment_index, container, false);

        return v;
    }

}
